package com.libbase

import android.content.ContextWrapper
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.libbase.cache.SPCache
import com.libbase.datarepository.ListDataRepository
import com.libbase.extension.*
import com.libbase.utils.DensityUtils
import com.libbase.utils.FileUtils
import com.libbase.utils.RomUtils
import org.junit.Assert.*
import org.junit.Test
import org.junit.runner.RunWith

/**
 * Instrumented test, which will execute on an Android device.
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
    @Test
    fun useAppContext() {
        // Context of the app under test.
        val appContext = InstrumentationRegistry.getInstrumentation().targetContext
        assertEquals("com.libbase.test", appContext.packageName)

        val listDataRepository = object : ListDataRepository<String>(null) {
            override val id: String
                get() = TODO("Not yet implemented")

            override fun decode(item: Any): String? {
                TODO("Not yet implemented")
            }

            override fun encode(item: String): Any {
                TODO("Not yet implemented")
            }

            override fun getKey(item: String): String {
                TODO("Not yet implemented")
            }
        }

        listDataRepository.dataObserver
    }

    @Test
    fun fileName_isCorrect() {
        assertEquals("test", "test".nameWithExtension)
        assertEquals(
            "test.pdf",
            FileUtils.instance.getNameWithExtension("test.pdf")
        )
        assertEquals(
            "test.pdf",
            FileUtils.instance.getNameWithExtension("file://test.com/test.pdf")
        )
        assertEquals(
            "test",
            FileUtils.instance.getNameWithExtension("file://test.com/test")
        )

        assertEquals("test", "test".nameWithoutExtension)
        assertEquals(
            "test",
            FileUtils.instance.getNameWithoutExtension("test.pdf")
        )
        assertEquals(
            "test",
            FileUtils.instance.getNameWithoutExtension("file://test.com/test.pdf")
        )
        assertEquals(
            "test",
            FileUtils.instance.getNameWithoutExtension("file:///test.pdf")
        )
        assertEquals(
            "test",
            FileUtils.instance.getNameWithoutExtension("file://test.com/test")
        )
    }

    @Test
    fun getCacheDir() {
        val appContext = InstrumentationRegistry.getInstrumentation().targetContext
        val cacheDir = FileUtils.instance.getDocumentsDir(ContextWrapper(appContext))
        assertNotNull(cacheDir)
    }

    @Test
    fun romUtils_test() {
        assertNotNull(RomUtils.name)
        assertEquals(false, RomUtils.isEmui)
        assertEquals(false, RomUtils.isSmartisan)
        assertEquals(false, RomUtils.isFlyme)
        assertEquals(false, RomUtils.isMiui)
        assertEquals(false, RomUtils.isSmartisan)
        assertEquals(false, RomUtils.isOppo)
        assertEquals(false, RomUtils.isVivo)
        assertEquals(false, RomUtils.is360())
    }

    @Test
    fun densityUtils_test() {
        val appContext = InstrumentationRegistry.getInstrumentation().targetContext

        var px = DensityUtils.instance.dp2px(appContext, 10F)
        assertEquals(10F, DensityUtils.instance.px2dp(appContext, px.toFloat()))

        px = DensityUtils.instance.sp2px(appContext, 10F)
        assertEquals(10F, DensityUtils.instance.px2sp(appContext, px.toFloat()))
    }

    @Test
    fun urlEncodeDecode_test() {
        assertEquals("https://www.baidu.com", "https://www.baidu.com".encodeURI())
        assertEquals("https%3A%2F%2Fwww.baidu.com", "https://www.baidu.com".encodeURIComponent())
        assertEquals("https://www.baidu.com", "https%3A%2F%2Fwww.baidu.com".decodeURI())
        assertEquals("https://www.baidu.com", "https%3A%2F%2Fwww.baidu.com".decodeURIComponent())
    }

    @Test
    fun appInfo_test() {
        val appContext = InstrumentationRegistry.getInstrumentation().targetContext

        appContext.packageManager.appIcon("")
    }

    @Test
    fun cache_test() {
        SPCache(InstrumentationRegistry.getInstrumentation().targetContext, "test")
    }
}